using System;
using System.Collections.Generic;
using System.Linq;

namespace TXK
{
    /// <summary>
    /// Int类型转换库
    /// </summary>
    public class IntLib
    {
        /// <summary>
        /// 字节数组中截取转成32位整型
        /// </summary>
        /// <param name="source"></param>
        /// <param name="start"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static int GetIntFromByteArray(byte[] source, int start = 0, DataFormat type = DataFormat.ABCD)
        {
            byte[] array = ByteArrayLib.Get4ByteArray(source, start, type);
            return (array != null) ? BitConverter.ToInt32(array, 0) : 0;
        }

        /// <summary>
        /// 将字节数组中截取转成32位整型数组
        /// </summary>
        /// <param name="source"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static int[] GetIntArrayFromByteArray(byte[] source, DataFormat type = DataFormat.ABCD)
        {
            int[] array = new int[source.Length / 4];
            for (int i = 0; i < source.Length / 4; i++)
            {
                array[i] = GetIntFromByteArray(source, 4 * i, type);
            }
            return array;
        }

        /// <summary>
        /// 将字符串转转成32位整型数组
        /// </summary>
        /// <param name="val"></param>
        /// <param name="spilt"></param>
        /// <returns></returns>
        public static int[] GetIntArrayFromString(string val, char spilt = ' ')
        {
            val = val.Trim();
            List<int> list = new List<int>();
            if (val.Contains(spilt))
            {
                string[] array = val.Split(new char[1] { spilt }, StringSplitOptions.RemoveEmptyEntries);
                string[] array2 = array;
                foreach (string text in array2)
                {
                    list.Add(Convert.ToInt32(text.Trim()));
                }
            }
            else
            {
                list.Add(Convert.ToInt32(val.Trim()));
            }
            return list.ToArray();
        }
    }
}
